import createAudioPermission from "./universal/js/create-audio-permission.js"

const audioPermissionInstance = createAudioPermission()


const palyButton = document.createElement('button')
palyButton.innerHTML = '浏览器播放'
palyButton.addEventListener('click', () => {
    audioPermissionInstance.requestPlaybackPermission().then(async () => {
        console.log('播放权限获取成功')

        const audioContext = new (window.AudioContext || window.webkitAudioContext)()

        const response = await fetch("https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3")
        const arrayBuffer = await response.arrayBuffer()
        const audioBuffer = await audioContext.decodeAudioData(arrayBuffer)

        const source = audioContext.createBufferSource()
        source.buffer = audioBuffer
        source.connect(audioContext.destination)
        source.start()

    }).catch((error) => {
        console.log(error.message)
    })
})
document.body.appendChild(palyButton)




const microphoneButton = document.createElement('button')
microphoneButton.innerHTML = '麦克风权限'
microphoneButton.addEventListener('click', () => {
    audioPermissionInstance.requestMicrophonePermission().then(() => {
        console.log('麦克风权限获取成功')

        navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
            const audioContext = new (window.AudioContext || window.webkitAudioContext)()
            const source = audioContext.createMediaStreamSource(stream)
            source.connect(audioContext.destination)
        })
    }).catch((error) => {
        console.log(error.message)
    })
})
document.body.appendChild(microphoneButton)
